import {
	reactive,
	ref
} from "vue";
import {
	QQMapWX
} from "@/utils/qqmap-wx-jssdk.js";

const QQMAPSDK = "JDVBZ-VDGWU-TLUV3-234XK-W57A6-JMBFW"

export function checkLocation() {
	uni.getSetting({
		success(res) {
			if (
				res.authSetting["scope.userLocation"] != undefined &&
				res.authSetting["scope.userLocation"] != true
			) {
				uni.showModal({
					title: "是否授权当前位置",
					content: "需要获取您的地理位置，请确认授权，否则获取不到门店",
					success: function(res) {
						if (res.cancel) {
							checkLocation();
						} else if (res.confirm) {
							uni.openSetting({
								success: function(data) {
									console.log(data);
									if (
										data.authSetting[
											"scope.userLocation"
										] == true
									) {
										uni.showToast({
											title: "授权成功",
											icon: "none",
											duration: 5000,
										});
										getLocation();
									} else {
										uni.showToast({
											title: "授权失败",
											icon: "none",
											duration: 5000,
										});
									}
								},
								fail: (err) => {
									console.log("open-", err);
								},
							});
						}
					},
					fail: (err) => {
						console.log("err-", err);
					},
				});
			}
		},
		fail: (err) => {
			console.log("open-", err);
		},
	});
};


//通过定位计算距离
export function getCalculateDistance(lat, lng) {
	console.log(lat, lng)
	return new Promise((resolve, reject) => {
		let qqmapsdk = new QQMapWX({
			key: QQMAPSDK,
		});
		uni.getLocation({
			type: "gcj02",
			success: (result) => {
				qqmapsdk.calculateDistance({
					mode: 'walking',
					from: {
						latitude: result.latitude,
						longitude: result.longitude,
					},
					to: [{
						latitude: lat,
						longitude: lng
					}],
					success: function(res: any) { //成功后的回调
						resolve(res)
					},
					fail: function(error: any) {
						reject(error)
					},
					complete: function(res: any) {
						console.log(res);
					}
				});
			}
		})


	})
};

//定位选择地址
export function getChooseLocation() {
	return new Promise((resolve, reject) => {
		uni.chooseLocation({
			success: (res) => {
				console.log(res)
				if (!res.address) {
					uni.showToast({
						title: "请选择位置",
						icon: "none",
					});
					return;
				}
				let qqmapsdk = new QQMapWX({
					key: QQMAPSDK,
				});
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude,
					},
					success: (succ: any) => {
						resolve({
							...succ,
							address: res.address,
							name: res.name
						})
					},
				});
			},
			fail(err) {
				reject(err)
			},
		});
	})
};

const location = reactive({
	longitude: 0,
	latitude: 0,
});

export function getLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: "gcj02",
			success(res) {
				resolve(res)
				location.longitude = res.longitude;
				location.latitude = res.latitude;
				uni.setStorageSync("location", JSON.stringify(res));
			},
			fail(err) {
				reject(err)
				if (err.errCode === 2) {
					uni.showToast({
						title: "请打开手机定位服务",
						icon: "none",
					});
				}
			},
		});
	})
};

// 通过经纬度定位，打开地图
export function openLocation(latitude: number, longitude: number, address) {
	return new Promise((resolve, reject) => {
		uni.openLocation({
			latitude: Number(latitude),
			longitude: Number(longitude),
			name: address,
			success(succ) {
				resolve(succ)
				console.log(succ)
			},
			fail(error) {
				reject(error)
				console.log(error)
			},
		})
	})
}